<!--
    AppleBuild.LocalBuild* files are meant for projects that want to use
    a local build of `dotnet/runtime` repo.

    Two ways to use this:

    1. Use a local runtime build from the source directory:
        Required properties to be set:

        $(RuntimeSrcDir)       - path to the runtime checkout. This should have already been
                                 built, and so should have `artifacts` directory.

        $(RuntimeConfig)       - This is the config that the above checkout was built with.
                                 This can be different from the configuration for the user project.

    2. Use a local directory which has all the tasks, targets, runtimepack available

        Required properties to be set:

        $(AppleBuildSupportDir)
-->
<Project>
  <Import Project="$(MSBuildThisFileDirectory)AppleBuild.targets" />
  <UsingTask TaskName="MonoAOTCompiler" AssemblyFile="$(MonoAOTCompilerTasksAssemblyPath)" />
  <UsingTask TaskName="RuntimeConfigParserTask" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />

  <PropertyGroup>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>link</TrimMode>
  </PropertyGroup>

  <PropertyGroup>
    <GenerateXcodeProject>true</GenerateXcodeProject>
    <GenerateCMakeProject>false</GenerateCMakeProject>
  </PropertyGroup>

  <!-- Use local runtime pack -->
  <Target Name="UpdateRuntimePack" AfterTargets="ResolveFrameworkReferences" DependsOnTargets="_PrepareAndValidateAppleInputs">
    <ItemGroup>
      <ResolvedRuntimePack PackageDirectory="$(MicrosoftNetCoreAppRuntimePackLocationToUse)"
                           Condition="'$(MicrosoftNetCoreAppRuntimePackLocationToUse)' != '' and
                                      '%(ResolvedRuntimePack.FrameworkName)' == 'Microsoft.NETCore.App'" />
    </ItemGroup>
    <Message Text="Used runtime pack: %(ResolvedRuntimePack.PackageDirectory)" Importance="high" />
  </Target>

  <!-- the actual properties need to get set in the props, so because UsingTasks depend on those. -->
  <Target Name="_PrepareAndValidateAppleInputs">
    <Error Condition="'$(RuntimeSrcDir)' == '' and '$(AppleBuildSupportDir)' == ''"
           Text="Both %24(RuntimeSrcDir) and %24(AppleBuildSupportDir) are not set. Either one of them needs to be set to use local runtime builds" />

    <Error Condition="'$(RuntimeSrcDir)' != '' and '$(AppleBuildSupportDir)' != ''"
           Text="Both %24(RuntimeSrcDir) and %24(AppleBuildSupportDir) are set. " />

    <Error Condition="'$(RuntimeSrcDir)' != '' and '$(RuntimeConfig)' == ''"
           Text="%24(RuntimeSrcDir) is set, but %24(RuntimeConfig) is not" />

    <PropertyGroup>
      <MicrosoftNetCoreAppRuntimePackDir>$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackLocationToUse)))</MicrosoftNetCoreAppRuntimePackDir>
      <MicrosoftNetCoreAppRuntimePackRidDir>$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackDir), 'runtimes', '$(TargetOS)-$(TargetArchitecture.ToLowerInvariant())'))</MicrosoftNetCoreAppRuntimePackRidDir>
      <AppleAppBuilderTasksAssemblyPath>$([MSBuild]::NormalizePath('$(AppleAppBuilderDir)', 'AppleAppBuilder.dll'))</AppleAppBuilderTasksAssemblyPath>
      <LibraryBuilderTasksAssemblyPath>$([MSBuild]::NormalizePath('$(LibraryBuilderDir)', 'LibraryBuilder.dll'))</LibraryBuilderTasksAssemblyPath>
      <MonoAOTCompilerTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoAOTCompilerDir)', 'MonoAOTCompiler.dll'))</MonoAOTCompilerTasksAssemblyPath>
      <MonoTargetsTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoTargetsTasksDir)', 'RuntimeConfigParser.dll'))</MonoTargetsTasksAssemblyPath>
    </PropertyGroup>

    <Error Condition="!Exists('$(MicrosoftNetCoreAppRuntimePackRidDir)')"
           Text="Cannot find runtime pack at $(MicrosoftNetCoreAppRuntimePackRidDir). Make sure you have the correct %24(RuntimeConfig) set" />
  </Target>
</Project>
